package com.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.core.guarantee.bo.GuaranteeBo;
import com.core.guarantee.dto.GuaranteeSearchCondition;
import com.core.guarantee.pojo.Guarantee;
import com.core.sysConfig.dao.SysConfigDao;

@Service
public class UploadUtil {
	protected Logger log= Logger.getLogger(this.getClass());
	@Resource
	private SysConfigDao sysConfigDao;
	@Resource
	private  GuaranteeBo guaranteeBo;
	public String  uploadFile(CommonsMultipartFile fileExcel,String txtId){
		String message ="";
//		保存路径按月份进行归类和临时路径
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
		String dateName = formatter.format(currentTime);
		String rootFold =sysConfigDao.getByKeyNo(Consts.FOLD_ROOT).getKey_value();
		String returnReportUploadFold =sysConfigDao.getByKeyNo(Consts.FOLD_RETURN_REPORT_UPLOAD).getKey_value();	
		String upSavePath=rootFold+File.separator+returnReportUploadFold;//上一层目录
		String savePath=rootFold+File.separator+returnReportUploadFold+File.separator+dateName;
//		判断该签证是否导入过
		GuaranteeSearchCondition sc = new GuaranteeSearchCondition();
		sc.setId(Long.parseLong(txtId));
		Guarantee guarantee=guaranteeBo.getById(sc);
		if(guarantee.getReturnReportUploadTime()!=null){
			String delDateName =formatter.format(guarantee.getReturnReportUploadTime());
			String delPath = upSavePath+File.separator+delDateName;//删除路径
			String delFile = delPath+File.separator+txtId+".xls";
			File file = new File(delFile);
			if (file.exists() && file.isFile()) {
	            if (file.delete()) {
	            	log.info("删除文件"+delFile+"成功!");
	            }
			}
		}
		File saveFileDir = new File(savePath);
		if (!saveFileDir.exists()) {
            // 创建保存目录
            saveFileDir.mkdirs();
        }
		String path=savePath+File.separator+txtId+".xls";
		if(!fileExcel.isEmpty()){  
            try {  
            	 String fileName = fileExcel.getName();
            	 System.out.println("文件名：" + fileName);
            	 fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);
                //定义输出流 将文件保存在D盘    file.getOriginalFilename()为获得文件的名字  
                FileOutputStream os = new FileOutputStream(path);
                InputStream in = fileExcel.getInputStream();  
                int b = 0;  
                while((b=in.read())!=-1){ //读取文件   
                    os.write(b);  
                }  
                os.flush(); //关闭流   
                in.close();  
                os.close();  
                sc.setReturnReportUploadTime(new Date());
                guaranteeBo.update(sc);
                  
            } catch (FileNotFoundException e) {  
                e.printStackTrace(); 
                return "102";
            } catch (IOException e) {  
                e.printStackTrace(); 
                return "102";
            } 
            
        }  
		return path;
	}
	
}
